package com.levi.login.util;

import javax.sql.DataSource;
import java.sql.*;

public class JdbcUtil {

    private static final String USERNAME = "root";
    private static final String PASSWORD = "mysql480";
    private static final String URL = "jdbc:mysql://localhost:3306/user_information";

    private static Connection connection = null;

    public static ResultSet execute(String sql, String... args) {
        if (connection == null) {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            try {
                connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, args[0]);
            ps.setString(2, args[1]);
            return ps.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }
}